package com.ecarx.camera.ui.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.camera.R;
import com.ecarx.camera.constant.Constants;
import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.utils.Utils;
import com.ecarx.camera.ui.BaseDialog;

import java.util.Arrays;

public class SelectionDialog extends BaseDialog {

    private static final String ARG_STRING_KEY = "selectionType";

    private TextView tvMessage;
    private RecyclerView rvSelection;

    private CharSequence message;
    private CharSequence[] txts;
    public boolean[] selectedStatus;

    @Constants.SelectionType
    private String type;
    public SelectionDialogAdapter adapter;

    public static SelectionDialog newInstance(@Constants.SelectionType String type) {
        SelectionDialog selectionDialog = new SelectionDialog();
        Bundle args = new Bundle();
        args.putString(ARG_STRING_KEY, type);
        selectionDialog.setArguments(args);
        return selectionDialog;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_selection;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initData() {
        super.initData();
        Bundle args = getArguments();
        if (args != null) {
            type = args.getString(ARG_STRING_KEY, Constants.SelectionType.SINGLE);
        }
        adapter = new SelectionDialogAdapter();
        adapter.setDataSource(type, txts, selectedStatus);
        adapter.setOnItemClickListener((position, selected) -> {
            if (Constants.SelectionType.SINGLE.equals(type)) {
                if (selected) {
                    for (int index = 0; index < selectedStatus.length; index++) {
                        if (selectedStatus[index]) {
                            selectedStatus[index] = false;
                            break;
                        }
                    }
                    selectedStatus[position] = true;
                    adapter.notifyDataSetChanged();
                }
            } else if (Constants.SelectionType.MULTIPLE.equals(type)) {
                selectedStatus[position] = selected;
                adapter.notifyDataSetChanged();
            }
            LogUtils.d(TAG, "onItemClick, selectedStatus = [" + Arrays.toString(selectedStatus) + "]");
        });
    }

    @Override
    protected void bindViews() {
        super.bindViews();
        tvMessage = layoutRoot.findViewById(R.id.tv_message);
        rvSelection = layoutRoot.findViewById(R.id.rv_selection);

        if (!TextUtils.isEmpty(message)) {
            tvMessage.setVisibility(View.VISIBLE);
            tvMessage.setText(message);
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        tvMessage.setTextColor(ContextCompat.getColor(Utils.getApp(), R.color.camera_base_day_night_fg_color));
        rvSelection.setAdapter(adapter);
    }

    public SelectionDialog setMessage(@NonNull CharSequence message) {
        this.message = message;
        return this;
    }

    public SelectionDialog setSelections(@NonNull CharSequence[] txts, @NonNull boolean[] selectedStatus) {
        this.txts = txts;
        this.selectedStatus = selectedStatus;
        return this;
    }
}
